/**
 * @Author       : cxh
 * @Date         : 2021-12-25 21:23:26
 * @LastEditTime : 2021-12-29 23:23:50
 * @LastEditors  : cxh
 * @Description  : 
 * @FilePath     : \vue_WorkSpace\vue-wpt\src\utils\request.js
 */
import {
    message
} from 'ant-design-vue'
// const PRE_URL = "http://127.0.0.1:3000/mock/11/wpt";
const PRE_URL = 'http://127.0.0.1:7001';

export const GET = (url, params={}) => {
    let targetUrl = PRE_URL + url;
    let str = '';
    for(let key in params){
        if(!str){
            str+=`?${key}=${params[key]}`
        }else{
            str+=`&${key}=${params[key]}`
        }
    }
    return fetch(targetUrl+str)
        .then(res=>res.json())
        .then(res=>{
            // const { data } = res || {};
            const { success, msg, data } = res || {};
            if(!success){
                message.error(msg);
                throw new Error(msg);
            }
            return data;
        })
}


// POST封装
export const POST = (url, params = {}) => {
    return fetch(PRE_URL+url, {
        method: 'POST',
        headers:{
            "Content-Type": "application/json",
        },
        body: JSON.stringify(params)
    })
      .then(res=>res.json())
      .then(res=>{
        // const { data } = res || {};
        const { success, msg, data } = res || {};
        if(!success){
            message.error(msg);
            throw new Error(msg);
        }
        return data;
    })
}



export default {
    POST,
    GET
}


